﻿
Imports System.IO
Imports System.Threading
Imports std
Imports xmlctrl
Imports xhtmlctrl
Imports webctrl
Imports storoomc

Public Class comment
    Inherits requestProcessor

    Public Const _pathidentity As String = "/comment"
    Public Const keyquery As String = "key"
    Public Const commentquery As String = "comment"
    Public Const idquery As String = "id"
    Public Const nocomment As String = "cannot get existing comment now."
    Protected maxline As Int64
    Protected linewidth As Int64

    Public Overrides Sub initial(Optional ByVal config As configParser = Nothing)
        MyBase.initial()
        maxline = config.parameter(configNaming.comment.name, configNaming.comment.maxline _
                                   , configNaming.defaultValue.comment.maxline)
        linewidth = config.parameter(configNaming.comment.name, configNaming.comment.linewidth _
                                   , configNaming.defaultValue.comment.linewidth)
    End Sub

    Public Overrides Function pathidentity() As String
        Return _pathidentity
    End Function

    Protected Function existingComment(ByVal key As String) As String
        Dim bs As bytes = Nothing
        Dim rtn As String = Nothing
        If storoom.read(key, bs) Then
            rtn = lastlines(encoding.utf8().GetString(bs.data), maxline, linewidth)
        Else
            raiseError("cannot read existing comment, key " + key + ", use default string." _
                       , errorHandle.errorType.warning)
            rtn = nocomment
        End If

        Return rtn
    End Function

    Protected Sub writeComment(ByVal i As Object)
        Dim input As pair(Of String, bytes) = Nothing
        input = convertor.cast(Of pair(Of String, bytes))(i)
        If Not storoom.write(input.first, input.second) Then
            raiseError("cannot write comment back to storoom, key " + input.first, errorHandle.errorType.warning)
        End If
    End Sub

    Public Overrides Function process(ByVal context As httpContext) As Boolean
        Dim buff As MemoryStream = Nothing
        buff = context.buff()
        Dim key As String = Nothing
        key = context.url.query(keyquery)

        Dim page As xhtmlpage = Nothing
        page = New xhtmlpage(buff)
        page.title().text = applicationSign() + " - comment"

        Dim csslink As csslink = Nothing
        csslink = New csslink(buff)
        csslink.href = csslinkhref
        page.head().addchild(csslink)

        Dim label As label = Nothing
        label = New label(buff)
        label.id = "id_existinglabel"
        label.text = "existing comments"
        page.body().addchild(label)

        Dim pre As pre = Nothing
        pre = New pre(buff)
        pre.id = "id_existing"
        Dim escapeText As escapeText = Nothing
        escapeText = New escapeText()
        escapeText = existingComment(key)
        pre.text = escapeText
        page.body().addchild(pre)

        Dim a As a = Nothing
        a = New a(buff)
        a.id = "id_comment_view"
        a.text = "view all comments"
        a.href = strmid(viewcomment._pathidentity, strlen(network.pathSeperator)) + network.questionMark _
                + viewcomment.keyquery + network.argumentNameValueSeperator + key
        a.target = "_blank"
        page.body().addchild(a)

        Dim form As form = Nothing
        form = New form(buff)
        form.action = strmid(pathidentity(), strlen(network.pathSeperator))
        form.method = xhtmlControl_1_1.method_enum.post
        form.id = "id_comment_form"
        page.body().addchild(form)

        label = New label(buff)
        label.id = "id_comment_label"
        label.text = "add your comment here, id "
        form.addchild(label)

        Dim input As input = Nothing
        input = New input(buff)
        input.type = xhtmlctrl.input.type_enum.text
        input.id = "id_comment_id"
        input.name = idquery
        form.addchild(input)

        input = New input(buff)
        input.type = xhtmlctrl.input.type_enum.text
        input.id = "id_comment_text"
        input.name = commentquery
        form.addchild(input)

        input = New input(buff)
        input.type = xhtmlctrl.input.type_enum.hidden
        input.name = keyquery
        input.value = key
        form.addchild(input)

        input = New input(buff)
        input.type = xhtmlctrl.input.type_enum.submit
        input.id = "id_comment_submit"
        input.value = "submit comment"
        form.addchild(input)

        context.setstatuscode(Net.HttpStatusCode.OK, "OK")
        context.setcontentEncoding(encoding.utf8)
        context.setcontentType(network.http.contentType.texthtmlUtf8)

        Dim comment As String = Nothing
        comment = context.url.query(commentquery)
        If Not isemptystring(comment) Then
            Dim id As String = Nothing
            id = context.url.query(idquery)
            If isemptystring(id) Then
                id = "unnaming"
            End If
            startThread(AddressOf writeComment, make_pair(key, New bytes(encoding.utf8().GetBytes( _
                                                            comment + " -<id> " + id _
                                                            + " -<ip> " + context.request.RemoteEndPoint.ToString() _
                                                            + " -<time> " + longTime() + newline.incode()))))
        End If

        If page.render() Then
            Return True
        Else
            raiseError("failed to render xhtmlpage, ignore request.", errorHandle.errorType.warning)
            Return False
        End If
    End Function
End Class
